<?php
/**
 * @author Adam Charron <adam.c@vanillaforums.com>
 * @copyright 2009-2023 Vanilla Forums Inc.
 * @license GPL-2.0-only
 */

namespace Vanilla\Forum\Widgets;

use Garden\Schema\Schema;
use Vanilla\Forms\NoCustomFragmentCondition;
use Vanilla\Layout\Asset\AbstractLayoutAsset;
use Vanilla\Layout\HydrateAwareInterface;
use Vanilla\Layout\HydrateAwareTrait;
use Vanilla\Utility\SchemaUtils;
use Vanilla\Web\TwigRenderTrait;
use Vanilla\Widgets\Fragments\OriginalPostFragmentMeta;
use Vanilla\Widgets\HomeWidgetContainerSchemaTrait;

class OriginalPostAsset extends AbstractLayoutAsset implements HydrateAwareInterface
{
    use HydrateAwareTrait;
    use TwigRenderTrait;
    use HomeWidgetContainerSchemaTrait;
    use PostCommentsAndOriginalPostTrait;

    public function getProps(): ?array
    {
        $props =
            [
                "discussion" => $this->getHydrateParam("discussion"),
                "discussionApiParams" => $this->getHydrateParam("discussionApiParams"),
                "category" => $this->getHydrateParam("category"),
                "page" => $this->getHydrateParam("page"),
            ] + $this->props;
        return $props;
    }

    public function renderSeoHtml(array $props): ?string
    {
        $result = $this->renderTwigFromString(
            <<<TWIG
<h1>{{discussion.name}}</h1>
<div>{{ renderSeoUser(discussion.insertUser) }}</div>
<div class="userContent">
{{discussion.body|raw}}
</div>
TWIG
            ,
            $props
        );
        return $result;
    }

    public static function getComponentName(): string
    {
        return "OriginalPostAsset";
    }

    public static function getWidgetIconPath(): ?string
    {
        return "/applications/dashboard/design/images/widgetIcons/OriginalPost.svg";
    }

    public static function getWidgetSchema(): Schema
    {
        return SchemaUtils::composeSchemas(
            self::widgetTitleSchema(defaultTitleType: "discussion/name"),
            self::authorBadgesSchema(),
            self::containerOptionsSchema(
                "containerOptions",
                minimalProperties: true,
                visualBackgroundType: "outer",
                defaultBorderType: "separator"
            )
        )->setField("properties.containerOptions.properties.headerAlignment.x-control", null);
    }

    public static function getWidgetName(): string
    {
        return "Original Post";
    }

    public static function getWidgetID(): string
    {
        return "asset.originalPost";
    }

    /**
     * @inheritdoc
     */
    public static function getFragmentClasses(): array
    {
        return [OriginalPostFragmentMeta::class];
    }
}
